<?

$message_continuer=">Continuer...";


$date_out_defaut=date("Y-m-d",next_open());
$date_in_defaut=date("Y-m-d",next_open()+24*3600);
$date_debut_defaut=date("Y-m-d",next_open());
$date_fin_defaut=date("Y-m-d",next_open()+24*3600);


//étape 3, création de la fiche
if ($_POST['step']==3)
	{
	$date_out=$_POST['date_out'];
	$date_in=$_POST['date_in'];
	if ($date_out>=$date_in)
		{
		print "ERREUR date de sortie plus grande ou égale à date de retour";
		$_POST['step']=2;

		}
	else
		{
		//insertion de la fiche
		$sql_insert_fiche_loc="INSERT INTO fiches_loc
		( `etat`, `date_debut`, `date_fin`, date_out, date_in, coef, `date_ajout`, `date_modif`, `admin`, `admin_modif`, `type_fact`)
		VALUES ('1','".$_POST['date_debut']."','".$_POST['date_fin']."','".$_POST['date_out']."','".$_POST['date_in']."','".$_POST['coef']."',NOW(),NOW(),'".$_POST['visa']."','".$_POST['visa']."','".$_POST['type_fact']."')";
		mysql_query($sql_insert_fiche_loc);
		$next_id=mysql_insert_id();
		
		//insertion des logs initiaux
		//état matériel
		$sql_insert_log = 'INSERT INTO `fiches_loc_etats_log` 
		( `id_fiches_loc`, `id_fiches_loc_etats`, `date`, `commentaire`, `id_admin`) 
		VALUES (\''.$next_id.'\', \'1\', NOW(),  \'création du devis\', \''.$_POST['visa'].'\');';
		mysql_query($sql_insert_log,$db);
		//type de contrat
		$sql_insert_log = 'INSERT INTO `fiches_loc_types_log` 
		( `id_fiches_loc`, `id_fiches_loc_type`, `date`, `commentaire`, `id_admin`) 
		VALUES (\''.$next_id.'\', \''.$_POST['type_fact'].'\', NOW(),  \'type de facture initial\', \''.$_POST['visa'].'\');';
		mysql_query($sql_insert_log,$db);
		
		//éléments séparateurs
		$sql_insert_fiche_loc="INSERT INTO fiches_loc_items 
		(id_fiche_loc, categorie, libelle) VALUES 
		('".$next_id."', '1', '---Sonorisation'),
		('".$next_id."', '2', '---Eclairage'),
		('".$next_id."', '3', '---Structures'),
		('".$next_id."', '4', '---Divers'),
		('".$next_id."', '6', '---Services')";
		
		mysql_query($sql_insert_fiche_loc,$db);
		//affiche la fiche que nous venons de créer
		$_GET['id_fiche_loc']=$next_id;
		include_once("inc.edit_fiche.php"); 
		}

	}

//étape 2, choix de la date de sortie et de retour

if ($_POST['step']==2)
	{
	if	($_POST['date_debut_radio']=="date_debut") $date_debut=$_POST['date_debut_Y']."-".$_POST['date_debut_m']."-".$_POST['date_debut_d'];
	else $date_debut=$_POST['date_debut'];
	if	($_POST['date_fin_radio']=="date_fin") $date_fin=$_POST['date_fin_Y']."-".$_POST['date_fin_m']."-".$_POST['date_fin_d'];
	else $date_fin=$_POST['date_fin'];
	
	if (!$_POST['visa'])
		{
		print "VISA obligatoire";
		$date_debut_defaut=$date_debut;
		$date_fin_defaut=$date_fin;
		$fond_cellule_visa=true;
		$_GET['step']=1;
		}
	elseif ($date_debut>=$date_fin)
		{
		print "ERREUR date de début plus grande ou égale à date de fin";
		$_GET['step']=1;
		$date_debut_defaut=$date_debut;
		$date_fin_defaut=$date_fin;
		$fond_cellule_fin=true;
		}
	else
		{
		$jours_location=(strtotime($date_fin)-strtotime($date_debut))/(24*60*60);
		$sql_coef="SELECT coef FROM coef WHERE floor<='".$jours_location."' AND roof>='".$jours_location."'";
		$res_coef=mysql_query($sql_coef,$db);
		$ligne_coef=mysql_fetch_assoc($res_coef);
		?>
        <form action="?nv_devis=1" method="post">
        dates d'utilisation: <?= format_date($date_debut,0)." au ".format_date($date_fin,0) ?><br>
        <table border="0"><tr><td>Date de sortie la plus proche:</td><td>
		<?= dates_select_highlite_openups(date('Y-m-d'), $date_debut, 0, previous_open($date_debut,'Y-m-d'), "date_out") ?>
        </td>
        </tr><tr><td>Date de retour la plus proche:</td><td>
        <?= dates_select_highlite_openups($date_fin,'', 360, next_open($date_fin,'Y-m-d'), "date_in") ?>

        </td></tr></table>
        Jours de location &gt;&gt;  <?=  $jours_location ?> &gt;&gt; coéficient <input name="coef" size="3" maxlength="5" type="text" value="<?= (float)$ligne_coef['coef'] ?>"><br>
        <input type="hidden" value="<?= $date_debut ?>" name="date_debut" />
        <input type="hidden" value="<?= $date_fin ?>" name="date_fin" />
        <input type="hidden" value="<?= $_POST['visa'] ?>" name="visa" />
        <input type="hidden" value="<?= $jours_location ?>" name="jours_location" />
        <input type="hidden" value="<?= $_POST['type_fact'] ?>" name="type_fact" />
        <input name="step" type="hidden" value="3" />
        <input name="nv_devis" type="hidden" value="1" />
        <input name="submit" type="submit" value="Continuer..." />
        </form>
        <? include ('inc.make_devis_coef_table.php'); ?>
        <?
		}

	}

//étape 1, choix de la date et affichage des devis en cours
if ($_GET['step']==1)
	{
	desactive_tous_vieux_devis();
	$_SESSION['id_fiche_loc']=FALSE;
	//print "devis actuellement en cours...<br><br>";
	?>
	<form action="?nv_devis=1" method="post">
	Introduction des dates d'utilisation<br><br>
	<table border="0">
    <tr><td>Date de début</td><td <? if ($fond_cellule_debut) {?> bgcolor="#FF0000" <? } ?> >
    <? dates_select_trois($date_debut_defaut, date("Y"), date("Y")+2, "date_debut"); ?>
	<input name="date_debut_radio" type="radio" value="date_debut">  <input name="date_debut_radio" type="radio" value="date_debut_auto" checked>
    <?= dates_select_highlite_openups(date('Y-m-d'), '', 360, $date_debut_defaut, "date_debut", FALSE) ?>
    
    
    
	</td></tr>
    <tr><td>Date de fin</td><td <? if ($fond_cellule_fin) { ?> bgcolor="#FF0000" <? } ?> >
    <? dates_select_trois($date_fin_defaut, date("Y"), date("Y")+2, "date_fin"); ?>
	<input name="date_fin_radio" type="radio" value="date_fin" >  <input name="date_fin_radio" type="radio" value="date_fin_auto" checked>
    <?= dates_select_highlite_openups(date('Y-m-d'), '', 360, $date_fin_defaut, "date_fin", FALSE) ?>
    
    
    
	</td></tr>
    <tr><td>Type de facture</td><td <? if ($fond_cellule_visa) { ?> bgcolor="#FF0000" <? } ?> >
    <? mk_list_table("type_fact","1","SELECT id, nom FROM fiches_loc_types WHERE active ='1' ORDER BY id", "id", "nom", $db, FALSE); ?>
	visa: <? mk_list_table("visa",0,"SELECT id, user FROM admin WHERE active= '1' ORDER BY user", "id", "user", $db, TRUE);?>
  OBLIGATOIRE </td></tr></table>
	<input name="step" type="hidden" value="2">
	<input name="nv_devis" type="hidden" value="1">
	<input name="submit" type="submit" value="Continuer..."><br>
</form>
	<table border=0><tr height=20><td><img src="../images/separator.gif" width="500" height="1"></td></tr></table>
<?
	$sql_look_fiche_loc="SELECT * FROM fiches_loc WHERE etat='1' ORDER BY date_out";
	$res_look_fiche_loc=mysql_query($sql_look_fiche_loc,$db);
	while($ligne_look_fiche_loc=mysql_fetch_array ($res_look_fiche_loc))
		{
		include('inc.loc_list_element.php');
		}
	}
?>